package com.wn.main.ui.system.viewmodel

import com.wn.common.model.ArticleList
import com.wn.main.ui.system.repository.ArticleListRepository
import com.wn.network.viewmodel.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2025/6/4 15:57
 * @Description: 体系中 viewpager2 对应的 Fragment -- 数据列表的 ViewModel
 */
@HiltViewModel
class ArticleListViewModel @Inject constructor(private val repository: ArticleListRepository) :
  BaseViewModel() {
  /**
   * 获取数据列表
   * @param mId Int
   * @param page Int
   * @return StateFlow<ArticleList?>
   */
  fun getArticleList(page: Int, mId: Int): StateFlow<ArticleList?> =
    callBaseRequest({ scope, errorCall, showLoading ->
      repository.getArticleList(
        scope,
        errorCall,
        showLoading,
        page,
        mId
      )
    })

  /**
   * 收藏 or 取消收藏站内文章
   * @param id Int                                           -- 文章 id
   * @param isCollect Boolean                                -- 是否收藏
   * @param isGetErrorCode Function2<Int?, String?, Unit>    -- 是否获取错误码
   * @return StateFlow<Any?>
   */
  fun collectArticle(
    id: Int,
    isCollect: Boolean,
    isGetErrorCode: ((Int?, String?) -> Unit),
  ): StateFlow<Any?> =
    callBaseRequest(
      { scope, errorCall, showLoading ->
        repository.collectArticle(
          scope,
          errorCall,
          showLoading,
          id,
          isCollect
        )
      },
      true,
      isGetErrorCode
    )
}